package hai.com.android_test.ui;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import hai.com.android_test.Bean.ContentType;
import hai.com.android_test.R;
import hai.com.android_test.base.BaseActivity;
import hai.com.android_test.mixed.DownloadCallback;
import hai.com.android_test.mixed.UploadRequestBody;
import hai.com.android_test.mixed.SimpleOkHttpCallBack;
import hai.com.android_test.utils.Helper;
import hai.com.android_test.utils.Logger;
import hai.com.android_test.utils.StringUtils;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpActivity extends BaseActivity {
    final int requeestcode_uploadfile = 100;
    String baseUri = "http://192.168.1.100:8080/webtest/";
    OkHttpClient client;
    @BindView(R.id.tv)
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
        ButterKnife.bind(this);
        client = Helper.getOkHttpClient();
    }

    public void click(View v) {
        switch (v.getId()) {
            case R.id.get:
                get("user/get.do");
                break;
            case R.id.button2:
                postForm("huang", "hai");
                break;
            case R.id.uploadFile:
                uploadFile();
                break;
            case R.id.downloadFile:
                downloadFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), "qinyunzhi2.mp4");
                break;
        }
    }

    void downloadFile(String dir, String fileName) {
        Request request = new Request.Builder().url("http://192.168.1.100:8080/qinyunzhi2.mp4").get().build();
        Helper.getOkHttpClient().newCall(request).enqueue(new DownloadCallback(dir, fileName) {
            @Override
            public void onProgress(long totalRead, long contentLength) {
                super.onProgress(totalRead, contentLength);
                Logger.e("downloading progress=" + StringUtils.floatScale(1.0 * totalRead / contentLength, 2));
            }
        });
    }

    void get(String suburi) {
        final Request request = new Request.Builder().url(baseUri + suburi).addHeader("head", "value").build();
        Call call = Helper.getOkHttpClient().newCall(request);
        call.enqueue(new SimpleOkHttpCallBack() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
            }
        });
    }

    void postForm(String name, String pwd) {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("username", name);
        builder.add("password", pwd);
        RequestBody requestBody = builder.build();
        Request request = new Request.Builder().url(baseUri + "user/post.do").post(requestBody).build();
        Call call = client.newCall(request);
        call.enqueue(new SimpleOkHttpCallBack());
    }

    void uploadFile() {
        File file1 = new File(Environment.getExternalStorageDirectory(), "webtest.zip");
        if (file1.exists())
            System.out.println("file name=" + file1.getName() + ",lenght=" + file1.length());
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        SimpleOkHttpCallBack simpleOkHttpCallBack = new SimpleOkHttpCallBack() {
            @Override
            public void onProgress(long sended, long total) {
                super.onProgress(sended, total);
                Log.e("onProgress", "onProgress() called with: sended = [" + sended + "], total = [" + total + "],progress=" + StringUtils.floatScale(1.0 * sended / total, 2));
            }
        };
        RequestBody requestBody = RequestBody.create(MediaType.parse(ContentType.multipart_form_data), file1);
        builder.addFormDataPart("file", file1.getName(), requestBody);
        builder.addFormDataPart("key", "value");
        Request request = new Request.Builder().url(baseUri + "file/uploadFile2.do").post(new UploadRequestBody(builder.build(), simpleOkHttpCallBack)).build();
        Call call = client.newCall(request);
        call.enqueue(simpleOkHttpCallBack);
    }
}
